//
//  NSObject+ResolveData.m
//  UI-lesson-17图片异步加载
//
//  Created by lanou3g on 15-3-10.
//  Copyright (c) 2015年 ncy. All rights reserved.
//

#import "NSObject+ResolveData.h"


@implementation NSObject (ResolveData)

+ (void)resolveWithUrlStr:(NSString *)urlStr method:(NSString *)method postBody:(NSString *)postBady myBlock:(myResolveBlock)rb
{
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  
   [request setHTTPMethod:method];
    if ([method isEqualToString:@"GET"]) {
       
                [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                    
                id tempObj = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
                    
                    rb(tempObj);
            
                    
                    
            
        }];
    }else if([method isEqualToString:@"POST"])
    {
        NSData *paramData = [postBady dataUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:paramData];
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            
            id tempObj = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
            
            rb(tempObj);
            
        }];
        
        
        
        
        
        
    }
    
    
    
//    NSMutableString *s = [NSMutableString string];
//    rb(s);
//    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
//        NSLog(@"%@",dict);
//    }];
}



@end

